package com.example.flowermall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.flowermall.entity.Category;
import com.example.flowermall.entity.Menu;
import com.example.flowermall.entity.Product;
import com.example.flowermall.mapper.CategoryMapper;
import com.example.flowermall.mapper.ProductMapper;
import com.example.flowermall.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoman
 * @since 2023-06-15
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    ProductMapper productMapper;
    @Override
    public List<Category> selectCategory(String categoryName) {
        return categoryMapper.selectCategory(categoryName);
    }

    @Override
    public void insertCategory(Category category) {
        categoryMapper.insert(category);

    }

    @Override
    public Category getCategoryById(Integer id) {
        Category category = categoryMapper.selectById(id);
        List<Product> products = productMapper.selectGoodsByCategoryId(id);
        category.setProducts(products);
        return category;
    }

    @Override
    public void updateCategory(Category category) {
        categoryMapper.updateById(category);
    }

    @Override
    public void deleteCategory(Integer id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public List<Category> selectByParentId(Integer id) {
        List<Category> categoryList = categoryMapper.selectByParentId(id);
        for(Category category:categoryList){
            List<Product> products=productMapper.selectGoodsByCategoryId(category.getCategoryId());
            category.setProducts(products);
        }
        return categoryList;
    }

    @Override
    public List<Category> getAllCategory() {

        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        List<Category> categoryList = categoryMapper.selectList(queryWrapper);
        for(Category category:categoryList){
            List<Product> products=productMapper.selectGoodsByCategoryId(category.getCategoryId());
            category.setProducts(products);
        }
        return categoryList;
    }

}
